<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Database\Mappers;

use NRG\Xnat as Xnat;
use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Framework\Exceptions\QueryException as QueryException;

/**
 * User mapper
 *
 * @package NRG\Database\Mappers
 */
class User extends Mapper
{
    /**
     * Constructor
     */
    public function __construct()
    {
	parent::__construct();
    }

    /**
     * Retrieve User object based on username and password
     *
     * @param string $accession Accession ID
     * @return MrSession
     */
    public function getUser($username=null, $password=null)
    {
        // input validation
        if(!is_string($username))
            throw new IllegalArgumentException("Username must be a string");
        else if($username == "")
            throw new IllegalArgumentException("Username cannot be null");

        if(!is_string($password))
            throw new IllegalArgumentException("Password must be a string");
        else if($password == "")
            throw new IllegalArgumentException("Password cannot be null");

        $username = $username;
        $password = $password;

        $password = $password;

        $values = array($username, $password);

        $query = "SELECT * FROM xdat_user WHERE login=$1 AND primary_password=$2";
        $result = $this->_xnat->query($query, $values);

        if(count($result) == 0)
            throw new QueryException(QueryException::EMPTY_RESULT, $query, $values);
        else if(count($result) > 1)
            throw new QueryException(QueryException::MULTI_ROW, $query, $values);

        $result = $result[0];

        $user = new Xnat\User();
        $user->setID((int) $result["xdat_user_id"]);
        $user->setUsername($result["login"]);
        $user->setEmail($result["email"]);

        return $user;
    }

    /**
     * Retrieve User object based on username and password
     *
     * @param string $accession Accession ID
     * @return MrSession
     */
    public function getUserByLogin($username)
    {
        // input validation
        if(!is_string($username))
            throw new IllegalArgumentException("Username must be a string");
        else if($username == "")
            throw new IllegalArgumentException("Username cannot be null");
        $values = array($username);

        $query = "SELECT * FROM xdat_user WHERE login=$1";
        $result = $this->_xnat->query($query, $values);

        if(count($result) == 0)
            return false;

        if(count($result) > 1)
            throw new QueryException(QueryException::MULTI_ROW, $query, $values);

        $result = $result[0];

        $user = new Xnat\User();
        $user->setID((int) $result["xdat_user_id"]);
        $user->setUsername($result["login"]);
        $user->setEmail($result["email"]);

        return $user;
    }



    /**
     * Get all email addresses for all users
     *
     * @return array
     */
    public function getAllEmailAddrs()
    {
	// query for all email addresses
	$query = "SELECT email from xdat_user WHERE enabled=1";
	$result = $this->_xnat->query($query, array(null));

	$emails = array();

	if(count($result) == 0)
	    return array();

	foreach($result as $item)
	    $emails[] = $item["email"];
	
	return $emails;
    }

    /**
     * Get all usernames for all users
     *
     * @return array
     */
    public function getAllUsernames()
    {
	// query for all email addresses
	$query = "SELECT login from xdat_user";
	$result = $this->_xnat->query($query, array(null));

	$logins = array();

	if(count($result) == 0)
	    return array();

	foreach($result as $item)
	    $logins[] = $item["login"];

	return $logins;
    }
}
